import * as NS from "../../Common/NetServices";
import { IsNull } from "../../TYLibrary/Common/GlobalExtend";
import { Wx_XSPPage, V_Category, V_Goods, T_Currency } from "../../Common/Entitys";
import { Log } from "../../TYLibrary/System/Logger";
import { Wx_PageData, WXDataClassRuntimeDecorator } from "../../Common/EntityBase";
import { TimeSpan } from "../../TYLibrary/System/System";

@WXDataClassRuntimeDecorator()
class IndexPageData extends Wx_PageData {
    public CategoryList: Array<V_Category> = new Array<V_Category>();
    public GoodsList: Array<V_Goods> = new Array<V_Goods>();
    public winHeight: string = ""; //窗口高度
    public currentTab: number = 0; //预设当前项的值
    public scrollLeft: number = 0; //tab标签的滚动条位置
    public scrollHeight: number = 0; //tab标签的滚动条高度
}

class IndexPage extends Wx_XSPPage {
    public Data: IndexPageData = new IndexPageData();

    /**
     * 初始化页面信息
     * @param {*} options
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async InitData(options: any): Promise<void> {
        // 底部加载
        this.EditTabBar();
        console.info("res.windowHeight");
        wx.getSystemInfo({
            success: async res => {
                console.info(res.windowHeight);
                res.windowWidth;
                this;
                this.Data.scrollHeight = res.screenHeight;
                await this.Data.ApplyChange(this);
            }
        });
        //分类目录
        this.App.CategoryList = await NS.QueryCategory();
        let s = this.App.CategoryList[0].ID;
        this.Data.CategoryList = this.App.CategoryList;
        this.Data.currentTab = 0;
        await this.Data.ApplyChange(this);
        await this.CategoryGoodsList();
    }

    /**
     * 类别商品列表
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async CategoryGoodsList(): Promise<void> {
        if (this.Data.currentTab == 0) {
            this.App.GoodsList = await NS.QuerySaleToDay();
        } else {
            this.App.GoodsList = await NS.QueryAllGoods(this.Data.currentTab, 1);
        }
        this.Data.GoodsList = this.App.GoodsList;
        this.Data.ApplyChange(this);
    }

    /**
     * 滚动切换标签样式
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async switchTab(e): Promise<void> {
        this.Data.currentTab = e.detail.current;
        this.Data.ApplyChange(this);
    }

    /**
     * 点击标题切换当前页时改变样式
     * @param {*} e
     * @returns {Promise<boolean>}
     * @memberof IndexPage
     */
    public async swichNavHandler(e): Promise<boolean> {
        var cur = e.target.dataset.current;
        if (this.Data.currentTab == cur) {
            return false;
        } else {
            this.Data.currentTab = cur;
            await this.Data.ApplyChange(this);
        }
        this.Data.scrollLeft = cur * 40 + 10;
        // this.checkCor();
        await this.Data.ApplyChange(this);
        await this.CategoryGoodsList();
    }

    // //判断当前滚动超过一屏时，设置tab标题滚动条。
    // checkCor() {
    //     if (this.data.currentTab >= 3) {
    //         this._SetData({ scrollLeft: 200 });
    //     } else {
    //         this._SetData({ scrollLeft: 0 });
    //     }
    // }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async scrolltolowerHandler(e: any): Promise<void> {}
}

Page(new IndexPage());
